/**
 * 订阅申请
 * @param tmplIds
 * @param callback
 */
export function subscribe(tmplIds,callback=null) {
    console.log('tmplIds', tmplIds)
    uni.getSetting({
        withSubscriptions: true,
        success(res) {
            console.log('withSubscriptions', res)
            var itemSettings = res.subscriptionsSetting.itemSettings
            if (itemSettings == null) {
                console.log('itemSettings为空')
                uni.showModal({
                    title: '提示',
                    content: '请授权开通服务通知',
                    showCancel: true,
                    success: function (ress) {
                        if (ress.confirm) {
                            uni.requestSubscribeMessage({
                                tmplIds: tmplIds,
                                success(res) {
                                    console.log('订阅消息 成功 ');
                                    console.log(res);
                                    return true;
                                },
                                fail(er) {
                                    console.log("订阅消息 失败 ");
                                    console.log(er);
                                    return false;
                                }
                            })

                        }
                        callback()
                    }
                })
            } else {
                console.log('itemSettings不为空',tmplIds)
                uni.requestSubscribeMessage({
                    tmplIds: tmplIds,
                    success: res => {
                        console.log('订阅消息 成功 ',res);
                        tmplIds.forEach(item => {
                            if (res[item] === 'reject') {
                                uni.showToast({
                                    title: '此次操作会导致您接收不到通知',
                                    icon: 'none'
                                })
                            }
                        })
                        callback()
                    },
                    fail: err => {
                        console.log('订阅消息 失败 ',err);
                        uni.showModal({   // uniapp框架
                            title: '请务必授权订阅消息',
                            content: '您没有授权消息通知，是否进行授权',
                            success: res => {
                                if (res.confirm) {
                                    uni.openSetting({
                                        success: res => {
                                            console.log(res)
                                        }
                                    })
                                } else if (res.cancel) {
                                    uni.showToast({
                                        title: '此次操作会导致您接收不到通知',
                                        icon: 'none',
                                        duration: 2000,
                                    })
                                }
                                callback()
                            },
                            fail: () => {
                                callback()
                            },
                            complete: () => {
                                callback()
                            }
                        });
                    },
                })
            }

        },
        fail: err => {
            console.log(err.errMsg)
            callback()
        },
    })
}
